﻿/**********************************************************************************
 * Description: Fjaspx!NT V2.0    
 * MemberVariable:
 * Functions:       
 * Modify information:     
 * 2009-8-5  New 杜耀辉  Create Class 
 * 
 * ********************************************************************************/
using System;
using System.Data;
using System.Web.UI.WebControls;
using Fjaspx.Common;
using System.IO;
using System.Xml;

namespace Fjaspx.UI.Web
{
    public partial class Administrator_Organization_add : AdminPage
    {

        #region GUID属性
        /// <summary>
        /// Gets or sets the GUID.
        /// </summary>
        /// <value>The GUID.</value>
        protected string GUID
        {
            get
            {
                return (string)ViewState["GUID"];
            }
            set
            {
                ViewState["GUID"] = value;
            }
        }
        #endregion
        #region userid属性
        /// <summary>
        /// Gets or sets the GUID.
        /// </summary>
        /// <value>The GUID.</value>
        protected string userid
        {
            get
            {
                return (string)ViewState["userid"];
            }
            set
            {
                ViewState["userid"] = value;
            }
        }
        #endregion
        /// <summary>
        /// Gets or sets the id
        /// </summary>
        /// <value>The int.</value>
        protected int id
        {
            get
            {
                return (int)ViewState["id"];
            }
            set
            {
                ViewState["id"] = value;
            }
        }
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                SystemCms.AdminSesion();
                if (this.GUID == null)
                {
                    this.GUID = System.Guid.NewGuid().ToString();
                }
                if (Fjaspx.Common.Session.Get("user_id") != null)
                {
                    this.userid = Fjaspx.Common.Session.Get("user_id");
                    if (!U_Exists(Convert.ToInt32(this.userid)))
                    {
                        this.Add();
                        MessageBox.Show(this, "首次登陆，成功创建！");
                    }
                    ShowInfo(Convert.ToInt32(this.userid));
                }
                else { Response.Redirect("/administrator/Message.aspx?message=<b>身份认证错误 Session.Get(user_id)为空 ！</b>&z=no"); }
            }
        }
        /// <summary>
        /// 判断用户是否注册了组织机构 U_s the exists.
        /// </summary>
        bool U_Exists(int userid)
        {
            Fjaspx.Sys.Bll.ROYcms_Organization bll = new Fjaspx.Sys.Bll.ROYcms_Organization();
            return bll.U_Exists(userid);
        }

        /// <summary>
        /// 绑定数据  Shows the info.
        /// </summary>
        /// <param name="id">The id.</param>
        private void ShowInfo(int userid)
        {
            Fjaspx.Sys.Bll.ROYcms_Organization bll = new Fjaspx.Sys.Bll.ROYcms_Organization();
            Fjaspx.Sys.Model.ROYcms_Organization model = bll.U_GetModel(userid);

            this.id = model.id;
            this.txttitle.Text = model.name;
            this.DdlMenu.SelectedValue = model.classname.ToString();
            this.keyword.Text = model.keyword;
            this.txtzhaiyao.Text = model.zhaiyao;
            
            this.txttag.Text = model.tag;
            FCKeditor1.Value = model.contents;
        }

        /// <summary>
        /// Updates this instance.
        /// </summary>
        public void Update()
        {
            int userID = Convert.ToInt32(this.userid);
            string name = this.txttitle.Text;

            int classname = int.Parse(this.DdlMenu.SelectedValue == "" ? "0" : this.DdlMenu.SelectedValue);
            string keyword = this.keyword.Text;
            string zhaiyao = this.txtzhaiyao.Text;
            string contents = FCKeditor1.Value;
            string tag = this.txttag.Text;


            int hits = 1;
            int orders = 1;

            int tuijian = 1;
            if (this.tuijian.Checked)
            {
                tuijian = 0;
            }
            int switchs = 1;
            if (this.switchs.Checked)
            {
                switchs = 0;
            }
            string type = "1";
            string Time = DateTime.Now.ToString();
            string GUID = this.GUID;


            Fjaspx.Sys.Model.ROYcms_Organization model = new Fjaspx.Sys.Model.ROYcms_Organization();
            model.id = this.id;
            model.userID = userID;
            model.name = name;
            model.classname = classname;
            model.keyword = keyword;
            model.zhaiyao = zhaiyao;
            model.contents = contents;
            model.tag = tag;
            model.hits = hits;
            model.orders = orders;
            model.tuijian = tuijian;
            model.switchs = switchs;
            model.type = type;
            model.Time = Time;
            model.GUID = GUID;

            Fjaspx.Sys.Bll.ROYcms_Organization bll = new Fjaspx.Sys.Bll.ROYcms_Organization();
            bll.Update(model);
            
        }
        /// <summary>
        /// 创建一个空的组织机构信息 Adds this instance.
        /// </summary>
        public void Add()
        {
            int userID = Convert.ToInt32(this.userid);
            string name = this.txttitle.Text;
            int classname = int.Parse(this.DdlMenu.SelectedValue == "" ? "0" : this.DdlMenu.SelectedValue);
            string keyword = this.keyword.Text;
            string zhaiyao = this.txtzhaiyao.Text;
            string contents = FCKeditor1.Value;
            string tag = this.txttag.Text;

 

            int hits = 1;
            int orders = 1;

            int tuijian = 1;
            if (this.tuijian.Checked)
            {
                tuijian = 0;
            }
            int switchs = 1;
            if (this.switchs.Checked)
            {
                switchs = 0;
            }
            string type = "1";
            string Time = DateTime.Now.ToString();
            string GUID = this.GUID;


            Fjaspx.Sys.Model.ROYcms_Organization model = new Fjaspx.Sys.Model.ROYcms_Organization();
            model.userID = userID;
            model.name = name;
            model.classname = classname;
            model.keyword = keyword;
            model.zhaiyao = zhaiyao;
            model.contents = contents;
            model.tag = tag;
            model.hits = hits;
            model.orders = orders;
            model.tuijian = tuijian;
            model.switchs = switchs;
            model.type = type;
            model.Time = Time;
            model.GUID = GUID;

            Fjaspx.Sys.Bll.ROYcms_Organization bll = new Fjaspx.Sys.Bll.ROYcms_Organization();
           int ID = bll.Add(model);
           if (ID != 1)
           {
               NewXml(ID);
           }

        }
        /// <summary>
        /// 创建XML 数据文件  News the XML.
        /// </summary>
        /// <returns></returns>
        bool NewXml(int ID)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", Config.ROYcmsConfig.Get_Config().templet_language, null);
                doc.AppendChild(dec);
                //创建一个根节点（一级） 
                XmlElement root = doc.CreateElement("Fjaspx");
                doc.AppendChild(root);
                //创建节点（二级） 
                XmlNode node = doc.CreateElement("Organization");
                XmlAttribute attr = null;
                attr = doc.CreateAttribute("Time");
                attr.Value = DateTime.Now.ToString();
                node.Attributes.SetNamedItem(attr);
                for (int i = 0; i < Request.Form.Count; i++)
                {
                    //Request.Form.Remove("");
                    //带有"<![CDATA[   ]]>"的元素 
                    XmlElement element = doc.CreateElement(Request.Form.GetKey(i));
                    //element.SetAttribute("Name", Request.Form.GetKey(i));
                    ////element.InnerText = "Round Comment";
                    XmlCDataSection idata = doc.CreateCDataSection("item");
                    idata.InnerText = Request.Form.Get(i);
                    element.AppendChild(idata);
                    node.AppendChild(element);
                }
                root.AppendChild(node);
                if (!Directory.Exists(Server.MapPath("~/APP_XML/Organization/")))
                {
                    Directory.CreateDirectory(Server.MapPath("~/APP_XML/Organization/")); //创建新路径 
                }
                doc.Save(Server.MapPath("~/APP_XML/Organization/" + ID + ".xml"));
                Console.Write(doc.OuterXml);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Handles the Click event of the Button_add control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Button_add_Click(object sender, EventArgs e)
        {
            Update();
            NewXml(this.id);
            Response.Redirect("/administrator/Message.aspx?message=<b>编辑信息成功！</b>&z=yes");
        }
        /// <summary>
        /// 自定义表单输出  Bind_s the from_ XML.
        /// </summary>
        public string From_Out()
        {
            try
            {
                return Fjaspx.Common.SystemCms.Read_File(Server.MapPath("~/App_Data/OrganizationFrom/" + this.id + ".html"), "utf-8");
            }
            catch
            {

                return "没有创建自定义表单项！";
            }
        }
        /// <summary>
        /// 导入表单模版 Handles the Click event of the Button_From_add control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Button_From_add_Click(object sender, EventArgs e)
        {
            if (FileUpload_From.HasFile)
            {
                if (!Directory.Exists(Server.MapPath("~/App_Data/OrganizationFrom/")))//判断是否存在
                {
                    Directory.CreateDirectory(Server.MapPath("~/App_Data/OrganizationFrom/"));//创建新路径 
                }
                FileUpload_From.SaveAs(Server.MapPath("~/App_Data/OrganizationFrom/" + this.id + ".html"));
            }
            else
            {
                Fjaspx.Common.MessageBox.Show(this, "请选择文件！");
            }
        }
    }
}